package com.nantong.admin.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.nantong.admin.domain.SysPermission;
import com.nantong.admin.domain.model.response.permission.SysPermissionVo;
import com.nantong.common.mybatis.core.mapper.BaseMapperPlus;

/**
 * 权限表 数据层
 *
 * @author nantong
 */
@Mapper
public interface SysPermissionMapper extends BaseMapperPlus<SysPermission, SysPermissionVo> {

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Select("SELECT DISTINCT p.* FROM sys_permission p " +
            "LEFT JOIN sys_role_permission rp ON rp.permission_id = p.permission_id " +
            "LEFT JOIN sys_user_role ur ON ur.role_id = rp.role_id " +
            "WHERE ur.user_id = #{userId}")
    List<SysPermission> selectPermissionsByUserId(@Param("userId") Long userId);

    /**
     * 根据角色ID查询权限
     *
     * @param roleId 角色ID
     * @return 权限列表
     */
    @Select("SELECT p.* FROM sys_permission p " +
            "LEFT JOIN sys_role_permission rp ON rp.permission_id = p.permission_id " +
            "WHERE rp.role_id = #{roleId}")
    List<SysPermission> selectPermissionsByRoleId(@Param("roleId") Long roleId);

    /**
     * 根据用户ID查询权限标识
     *
     * @param userId 用户ID
     * @return 权限标识列表
     */
    @Select("SELECT DISTINCT p.permission_key FROM sys_permission p " +
            "LEFT JOIN sys_role_permission rp ON rp.permission_id = p.permission_id " +
            "LEFT JOIN sys_user_role ur ON ur.role_id = rp.role_id " +
            "WHERE ur.user_id = #{userId}")
    List<String> selectPermissionKeysByUserId(@Param("userId") Long userId);

    List<SysPermission> listPermissionByUserId(@Param("userId") Long userId);

    List<Long> listPermissionByRoleId(@Param("roleId") Long roleId);
}
